<?php

namespace App\Repository\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class ProductCategories.
 *
 * @package namespace App\Repository\Models;
 */
class ProductSkus extends Model implements Transformable
{
    use TransformableTrait;
    use SoftDeletes;

    protected $table = 'product_skus';
    
    protected $fillable = ['product_id', 'sku_code', 'price', 'discount', 'stock', 'status','sale_price','seller_code',
        'attributes', 'freight_type', 'freight_template_id', 'bulk', 'weight','freight'];

    protected $appends = ['final_price'];

//    protected $hidden = ['price'];


/**
    * 搜索
    * @var array
    */
   protected $fieldSearchable = [
       'final_price' => 'between',
   ];

    /**
     * 最终价格，前端取数据全部通过这个
     */
    public function getFinalPriceAttribute()
    {
        $price = array_get($this->attributes, 'price');
        if($discount = array_get($this->attributes, 'discount')){
            return $price * $discount;
        }
        return $price ? : '-';
    }

    public function product()
    {
        return $this->hasOne(Products::class, 'id', 'product_id');
    }

}
